package jh.com.jhappbf;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;


import jh.com.jhappbf.Interface.GetItemClick;

/**
 * 作者：Created by GP on 2020/8/1.
 */
public class ComfirmListAdapter extends BaseAdapter {
    private ArrayList<String> mData;
    private ArrayList<String> mDataZiDuan;

    private Context context;
    private GetItemClick getItemClick;
    private HashMap<String, String> contents = new HashMap<>();

    public void setContents(HashMap<String, String> contents) {
        this.contents = contents;
    }

    public HashMap<String, String> getContents() {
        return contents;
    }

    public ComfirmListAdapter(Context context, ArrayList<String> mData,ArrayList<String> mDataZiDuan, GetItemClick getItemClick) {
        this.context = context;
        this.mData=mData;
        this.mDataZiDuan=mDataZiDuan;
        this.getItemClick=getItemClick;
    }


        @Override
        public int getCount() {
            return mData.size()==0?0:mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewlHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.list_view_item_edittext_textview, parent,false);
                holder = new ViewlHolder();
                holder.mTxt = (TextView) convertView
                        .findViewById(R.id.list_view_item_checkbox_textView_txt);
                holder.mEdt = (EditText) convertView
                        .findViewById(R.id.list_view_item_edittext_textView_chk);
                holder.mImg = (ImageView)convertView.findViewById(R.id.select_img);
                convertView.setTag(holder);

            } else {
                holder = (ViewlHolder) convertView.getTag();
            }
            String rightName = mData.get(position);
            holder.mImg.setVisibility(View.INVISIBLE);
            holder.mEdt.setEnabled(false);
            if (TextUtils.equals("订单号",rightName)){
                holder.mImg.setVisibility(View.VISIBLE);
            }
            holder.mImg.setOnClickListener(v -> getItemClick.getImgClick(position));
            if (TextUtils.equals("扣杂",rightName)||TextUtils.equals("地点",rightName)){
                holder.mEdt.setEnabled(true);
                if (TextUtils.equals("扣杂",rightName)){
                    holder.mEdt.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                }
            }
            holder.mTxt.setText(rightName);
            holder.mEdt.addTextChangedListener(new MyTextChangedListener(holder));
            if (!TextUtils.isEmpty(contents.get(holder.mTxt.getText().toString()))){
                holder.mEdt.setText(contents.get(holder.mTxt.getText().toString()));
            }else {
                holder.mEdt.setText("");
            }
            return convertView;
        }

    class ViewlHolder {
        public TextView mTxt;
        public EditText mEdt;
        public ImageView mImg;
    }


    class MyTextChangedListener implements TextWatcher {
        ViewlHolder holder;

        public MyTextChangedListener(ViewlHolder holder) {
            this.holder = holder;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (holder != null && contents != null) {
                String key = holder.mTxt.getText().toString();
                contents.put(key, editable.toString());
            }
        }
    }
}



